package com.springcloud.user_sso.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.springcloud.user_sso.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * com.springcloud.user_sso.service;
 * 功能：;
 *
 * @author 李冉 Email:1828581413@qq.com
 * Time:2018/4/5 21:06
 */
@Service
public class SsoService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "checkUserFallbackMethod")//进行容错处理
    public User checkUser(String username, String password) {
        try {
            String serveId = "springcloud-user-service";
            User user = this.restTemplate.getForObject("http://" + serveId + "/user/" + username, User.class);
            if (user != null) {
                String pwd = user.getPassword();
                if (pwd.equals(password)) {
                    return user;
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public User checkUserFallbackMethod(String username, String password) {
        return new User();
    }


}
